class DevicesController < ApplicationController
  before_action :set_device, only: [:show, :edit, :update, :destroy]

  # GET /devices
  # GET /devices.json
  def index
    @devices = Device.where(status: 1).paginate page: params[:page], per_page: 20

    page_count = params[:page].nil? ? 1 : params[:page]
    @page_index = (page_count.to_i - 1) * 20
  end

  # GET /devices/1
  # GET /devices/1.json
  def show
  end

  # GET /devices/new
  def new
    @device = Device.new

    prepare_for_options
  end

  def prepare_for_options
    @os_type_options = ['Windows', 'Linux']
    @os_bit_options = ['32-bit', '64-bit']
    @browser_version_options = ['Any', 'IE6', 'IE7', 'IE8', 'IE9', 'IE10', 'IE11', 'FireFox']
    @browser_bit_options = ['32-bit', '64-bit']
  end

  # GET /devices/1/edit
  def edit
    p @device.os_type

    prepare_for_options
  end

  # GET /devices/query
  def query
    @os_type = params[:os_type].strip
    @ip = params[:ip].strip
    
    if @os_type != 'ANY'
      @devices = Device.where(status: 1, os_type: @os_type).paginate page: params[:page], per_page: 20
    else
      if @ip.empty?
        @devices = Device.where(status: 1).paginate page: params[:page], per_page: 20
      else
        @devices = Device.where(status: 1, ip: @ip).paginate page: params[:page], per_page: 20
      end
    end

    page_count = params[:page].nil? ? 1 : params[:page]
    @page_index = (page_count.to_i - 1) * 20
    
    respond_to do |format|
      format.html { render :index }
    end
  end

  # GET /devices/require
  def require
    @device = Device.new

    prepare_for_options
  end

  # GET /devices/apply
  def apply
    @devices = Device.where(status: 0).paginate page: params[:page], per_page: 20
  end

  # POST /devices
  # POST /devices.json
  def create
    @device = Device.new(device_params)

    respond_to do |format|
      if @device.save
        format.html { redirect_to @device, notice: 'save device success.' }
        format.json { render :show, status: :created, location: @device }
      else
        format.html { render :new }
        format.json { render json: @device.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /devices/1
  # PATCH/PUT /devices/1.json
  def update
    respond_to do |format|
      if @device.update(device_params)
        format.html { redirect_to @device, notice: 'update device success.' }
        format.json { render :show, status: :ok, location: @device }
      else
        format.html { render :edit }
        format.json { render json: @device.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /devices/1
  # DELETE /devices/1.json
  def destroy
    @device.destroy
    respond_to do |format|
      format.html { redirect_to devices_url, notice: 'destroy device success.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_device
      @device = Device.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def device_params
      params.require(:device).permit(:ip, :os_type, :os_name, :os_bit, :browser_version, :browser_bit, :owner, :description, :remark, :status)
    end
end
